package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    private TextView textMarquee;
    private Button btnTest;
    private ToggleButton toggleBtn;
    private RadioGroup rg;
    private TextView textRadioResult;
    private Button btnProgressBar;
    private Button btnBasicDialog;
    private Button btnMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnTest = findViewById(R.id.btn_test);
        toggleBtn = findViewById(R.id.toggleBtn);
        rg = findViewById(R.id.rg);
        textRadioResult = findViewById(R.id.text_radio_result);
        btnProgressBar = findViewById(R.id.btn_progress_bar);
        btnBasicDialog = findViewById(R.id.btn_basic_dialog);

        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("按钮事件", "单击了按钮");
            }
        });
        btnTest.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                String msg = "触摸事件";
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        Log.i(msg, "松开按钮");
                        toggleBtn.setChecked(false);
                        break;
                    case MotionEvent.ACTION_DOWN:
                        Log.i(msg, "按下按钮");
                        toggleBtn.setChecked(true);
                        break;
                }
                return false;
            }
        });

        toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("选中状态", isChecked + "");
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.i("CheckedId", rg.getCheckedRadioButtonId()+"--"+checkedId );
                RadioButton checkedRadioBtn = findViewById(checkedId);
                CharSequence text = checkedRadioBtn.getText();
                textRadioResult.setText("你选择的是："+text);
            }
        });

        btnProgressBar.setOnClickListener(new ClickListener(MainActivity2.class));
        btnBasicDialog.setOnClickListener(new ClickListener(BasicDialogActivity.class));

        btnMenu = findViewById(R.id.btn_menu);
        btnMenu.setOnClickListener(new ClickListener(MenuActivity.class));

    }

    private class ClickListener implements View.OnClickListener{
        private Class aClass;

        public ClickListener(Class aClass) {
            this.aClass=aClass;
        }
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),aClass);
            startActivity(intent);
        }
    }

}